package com.meilai.project.util.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

/**
 * @author 张驰
 * @date 2021/11/22 10:33
 */
public class BuildingAreaImportModelUtil {
    public static XSSFWorkbook getModel(String sheetName, String[] title, String[] remark){
        int remarkStartRowIndex = 10;
        //新建文档实例
        XSSFWorkbook wb = new XSSFWorkbook();

        //在文档中添加表单
        XSSFSheet sheet = wb.createSheet(sheetName);
        for(int i=0;i<remark.length;i++){ // 备注行
            sheet.addMergedRegion(new CellRangeAddress(i+remarkStartRowIndex,i+remarkStartRowIndex,0,10));
        }

        for (int i = 0; i < title.length; i++) {
            sheet.setColumnWidth(i, 20 * 256);
        }

        // 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        XSSFRow row0 = sheet.createRow(0);

        // 创建单元格，并设置值表头 设置表头居中
        Font headFont = wb.createFont();
        headFont.setFontHeightInPoints((short) 12);
        headFont.setColor(IndexedColors.BLACK.getIndex());
        XSSFCellStyle headCellStyle = wb.createCellStyle();
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT
                .getIndex());
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        headCellStyle.setFont(headFont);

        // 备注样式
        Font remarkFont = wb.createFont();
        remarkFont.setFontHeightInPoints((short) 10);
        remarkFont.setColor(IndexedColors.BLACK.getIndex());
        XSSFCellStyle remarkStyle = wb.createCellStyle();
        remarkStyle.setAlignment(HorizontalAlignment.LEFT);
        remarkStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        remarkStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        remarkStyle.setFont(remarkFont);

        //声明列对象
        XSSFCell cell = null;

        //创建标题
        for(int i=0;i<title.length;i++){
            cell = row0.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(headCellStyle);
        }


        for(int i=0;i<remark.length;i++){ // 备注行，第4开始
            XSSFRow row = sheet.createRow(i+remarkStartRowIndex);
            cell = row.createCell(0);
            cell.setCellValue(remark[i]);
            cell.setCellStyle(remarkStyle);
        }

        return wb;
    }

    private static XSSFCellStyle createBorderedStyle(XSSFWorkbook wb) {
        XSSFCellStyle style = wb.createCellStyle();
        style.setBorderRight(BorderStyle.THIN);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderBottom(BorderStyle.THIN);
        style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        return style;
    }
}
